package controller;

import Service.HouseService;
import Utils.PageSplit;
import com.opensymphony.xwork2.ActionSupport;
import entity.House;
import entity.Street;
import entity.Types;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HouseListAction extends ActionSupport {
    private String mianji;
    private String jiage;
    private String fangxing;
    private String stree;
    private String uname;
    private List<House> houseList= null;

    public String execute(){
        House house = new House();
        PageSplit pageSplit = new PageSplit();
        pageSplit.setCurrentNo(0);
        pageSplit.setPerNum(2);
        pageSplit.setMaxArea(600);
        pageSplit.setMinArea(0);
        pageSplit.setMaxPrice(10000);
        pageSplit.setMinPrice(0);
        pageSplit.setHouse(house);
        pageSplit.setStreet(new Street());
        pageSplit.setTypes(new Types());
        HouseService houseService = new HouseService();
        houseList = houseService.houseList(pageSplit);
        return SUCCESS;
    }
    public String searchHouseList(){
        System.out.println("这是uname"+uname);
        int uname1=0;
        House house = new House();
        if (isNumeric(uname)){
            uname1 = Integer.parseInt(uname);
            house.setHouseId(uname1);
        }else {
            house.setHouseTitle(uname);
        }
        PageSplit pageSplit = new PageSplit();
        pageSplit.setCurrentNo(0);
        pageSplit.setPerNum(2);
        pageSplit.setMaxArea(600);
        pageSplit.setMinArea(0);
        pageSplit.setMaxPrice(10000);
        pageSplit.setMinPrice(0);
        pageSplit.setHouse(house);
        pageSplit.setStreet(new Street());
        pageSplit.setTypes(new Types());
        HouseService houseService = new HouseService();
        houseList = houseService.houseList(pageSplit);
        return SUCCESS;

    }
    //通过其他几个条件进行搜索：价格房型等等
    public String srearchByprice(){
       //价格
        String[] strPrice =  jiage.split("-");
        double miPrice = Double.parseDouble(strPrice[0]);
        double maPrice = Double.parseDouble(strPrice[1]);
        System.out.println(miPrice+";;;;;;;"+maPrice);
       //面积
        String[] strArea =  mianji.split("-");
        double miArea = Double.parseDouble(strArea[0]);
        double maArea = Double.parseDouble(strArea[1]);

        House house = new House();
        PageSplit pageSplit = new PageSplit();
        pageSplit.setCurrentNo(0);
        pageSplit.setPerNum(2);
        pageSplit.setMaxArea(maArea);
        pageSplit.setMinArea(miArea);
        pageSplit.setMaxPrice(maPrice);
        pageSplit.setMinPrice(miPrice);
        pageSplit.setHouse(house);

        Street street = new Street();
        street.setStreetName(stree);
        pageSplit.setStreet(street);
        pageSplit.setTypes(new Types());
        HouseService houseService = new HouseService();
        houseList = houseService.houseList(pageSplit);
        return SUCCESS;
    }
//    判断是否为整数
    public boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }
    public List<House> getHouseList() {
        return houseList;
    }

    public void setHouseList(List<House> houseList) {
        this.houseList = houseList;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getMianji() {
        return mianji;
    }

    public void setMianji(String mianji) {
        this.mianji = mianji;
    }

    public String getJiage() {
        return jiage;
    }

    public void setJiage(String jiage) {
        this.jiage = jiage;
    }

    public String getFangxing() {
        return fangxing;
    }

    public void setFangxing(String fangxing) {
        this.fangxing = fangxing;
    }

    public String getStree() {
        return stree;
    }

    public void setStree(String stree) {
        this.stree = stree;
    }
}
